{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## github 地址：\n",
    "## https://github.com/05bit/peewee-async\n",
    "\n",
    "## PostgreSQL peewee库安装\n",
    "pip install --pre peewee-async\n",
    "pip install aiopg\n",
    "\n",
    "## MySQL peewee库安装\n",
    "pip install --pre peewee-async\n",
    "pip install aiomysql\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 模型定义\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "from peewee import (\n",
    "    MySQLDatabase,\n",
    "    Model,\n",
    "    CharField,\n",
    "    DateField,\n",
    "    IntegerField,\n",
    "    FloatField,\n",
    "    TextField,\n",
    "    DateTimeField,\n",
    "    ForeignKeyField,\n",
    ")\n",
    "import peewee_async\n",
    "\n",
    "## 异步数据库连接\n",
    "async_db = peewee_async.MySQLDatabase(\n",
    "    'message', host='127.0.0.1', port=3308, user='root', password=''\n",
    ")\n",
    "objects = peewee_async.Manager(async_db)\n",
    "async_db.set_allow_sync(False)\n",
    "\n",
    "class BaseModel(Model):\n",
    "    add_time = DateTimeField(default=datetime.now, verbose_name='添加时间')\n",
    "    class Meta:\n",
    "        database = db\n",
    "\n",
    "\n",
    "class Supplier(BaseModel):\n",
    "    name = CharField(max_length=100, verbose_name='名称', index=True)\n",
    "    address = CharField(max_length=100, verbose_name='联系地址')\n",
    "    phone = CharField(max_length=11, verbose_name='联系方式')\n",
    "    class Meta:\n",
    "        database = db\n",
    "        table_name = 'Supplier'\n",
    "\n",
    "\n",
    "class Goods(BaseModel):\n",
    "    supplier = ForeignKeyField(Supplier, verbose_name='商家', backref='goods')\n",
    "    name = CharField(max_length=100, verbose_name='商品名称', index=True)\n",
    "    click_num = IntegerField(default=0, verbose_name='点击数')\n",
    "    goods_num = IntegerField(default=0, verbose_name='库存数')\n",
    "    price = FloatField(default=0.0, verbose_name='价格')\n",
    "    brief = TextField(verbose_name='商品简介')\n",
    "\n",
    "    class Meta:\n",
    "        database = db\n",
    "        table_name = 'goods'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 模型调用\n",
    "\n",
    "import asyncio\n",
    "\n",
    "from ch07peewee.models.async_model import objects, Goods\n",
    "\n",
    "\n",
    "async def handler():\n",
    "    ## 插入数据\n",
    "    # await objects.create(Goods, supplier_id=4, name='100度水井坊臻酿八號500ml',\n",
    "    #                      click_num=20, goods_num=1000, price=500, brief='xxxxx茅台酒厂')\n",
    "    \n",
    "    ## 查询数据\n",
    "    goods = await objects.execute(Goods.select())\n",
    "    for good in goods:\n",
    "        print(good.name)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    loop = asyncio.get_event_loop()\n",
    "    loop.run_until_complete(handler())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
